<template>
    <div class='fxn-body'>
        <div class="fxn-top" v-if="authMenu('sys_dep_add')">
            <el-button type="primary" icon="Plus" @click="handleOp(1)">添加</el-button>
        </div>
        <!-- Top End -->
        <div class="fxn-section fxn-margin-t-20">
            <el-table :data="listData" row-key="id" border default-expand-all :tree-props="{ children: 'children' }">
                <el-table-column label="部门名称" prop="name" />
                <el-table-column label="状态" prop="status" width="80">
                    <template #default="scope">
                        <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" :disabled="!authMenu('sys_dep_status')" @change="handleStatus(scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120">
                    <template #default="scope">
                        <el-link v-if="authMenu('sys_dep_edit')" type="primary" :underline="false" @click="handleOp(2,scope.row)">编辑</el-link>
                        <el-link v-if="authMenu('sys_dep_del')" type="danger" :underline="false" @click="handleDel(scope.row)">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- Section End -->

        <!-- 操作 -->
        <dialogForm ref="dialogData" :formData="formData" @handleDialog="handleDialog"></dialogForm>
        <!-- 操作 End -->
    </div>
</template>

<script>
    import dialogForm from '../Common/Form.vue'
    import { defineComponent, reactive, getCurrentInstance, onMounted, toRefs, computed } from 'vue'
    import { useRouter } from 'vue-router'
    import { useStore } from 'vuex'
    export default defineComponent({
        components: {dialogForm},
        setup() {
            const { proxy } = getCurrentInstance();
            const router = useRouter();
            const store = useStore();

            //初始化数据
            const state = reactive({
                loading: false,
                listData: [],
                files: [],

                //操作
                formData: {
                    inline: false,
                    width: '800',
                    labelWidth: '120',
                    data: [
                        {formType: 'cascade', label: '上级部门', prop: 'pid', data: [], props: {label: 'name',value: 'id', children: 'children', checkStrictly: true,}, rules: [{ required: true, message: '请选择上级部门', trigger: 'blur' }], placeholder: '请选择上级部门'},
                        {formType: 'input', label: '部门名称', prop: 'name', maxlength: '6', rules: [{ required: true, message: '请输入部门名称', trigger: 'blur' }], placeholder: '请输入部门名称'},
                        {formType: 'input', type: 'textarea', rows: 5, label: '备注', prop: 'desc', placeholder: '请输入备注'},
                    ]
                },
            })

            //列表
            const getList = () => {
                state.loading = true;
                proxy.$http.get('/oa/Department/index')
                .then(res=> {
                    state.loading = false;
                    const { code, msg ,data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        state.listData = data;
                    }
                })
            }

            //操作
            const handleOp = (type,row) => {
                let id = '';
                if(type == 2){
                    id = row.id;
                }
                proxy.$http.get('/oa/Department/op',{id: id})
                .then(res=> {
                    const { code, msg ,data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        let array = state.formData.data;
                        array.forEach((item)=> {
                            if(item.prop == 'pid'){
                                item.data = data._departments_;
                            }
                        })
                        state.formData.data = array;
                        if(type == 1){
                            state.formData = {
                                ...state.formData,
                                title: '添加',
                            }
                        }else{
                            state.formData = {
                                ...state.formData,
                                title: '编辑',
                                model: data
                            }
                        }
                        proxy.$refs.dialogData.openDialog();
                    }
                })
            }

            //提交操作
            const handleDialog = (e) => {
                //判断上级是否存在
                if(e.pid){
                    e.pid = e.pid[e.pid.length -1];
                }
                proxy.$http.post('/oa/Department/op', e )
                .then(res=>{
                    const { code, msg , data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        proxy.$msg.success('修改成功!');
                        getList();
                    }
                })
            }

            //修改状态
            const handleStatus = (row) => {
                proxy.$http.post('/oa/Department/state',{id: row.id, status:row.status})
                .then(res=>{
                    const { code, msg , data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        proxy.$msg.success('修改成功!');
                        getList();
                    }
                })
            }

            //删除
            const handleDel = (row) => {
                proxy.$confirm("此操作将会删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                .then(async () => {
                    proxy.$http.post('/oa/Department/remove',{id: row.id})
                    .then(res=> {
                        let { code, msg, data } = res;
                        if(code != 1){
                            return proxy.$msg.error(msg);
                        }else{
                            proxy.$msg.success('删除成功!');
                            getList();
                        }
                    })
                })
                .catch(() => {})
            }

            //获取权限
            const authMenu = (value) => {
                let currentRouter = router.currentRoute.value.path;
                let menuArr = (proxy.$public.authMenu(currentRouter).split(','));
                for (let index = 0; index < menuArr.length; index++) {
                    if(value == menuArr[index]){
                        return true
                    }
                }
            }
            
            //Dom加载
            onMounted(()=>{
                authMenu();
                getList();
            })
            
            return {
                ...toRefs(state),
                
                //请求方法
                getList,
                handleOp,
                handleDialog,
                handleStatus,
                handleDel,
                
                //组件方法
                authMenu,
            }

        },
    })
</script>

<style scoped>
    
</style>